#include "socket_client.h"
#include <iostream>
#include <string>
#include "serial.h"

int main(int argc, char *argv[])
{
    std::string client_id = "Anke2101_安科测试一店_安科测试一店男更";
    SocketClient client;

    // 配置自动重连 - 无限重试，初始延迟1秒，最大延迟30秒
    client.setAutoReconnect(true, -1, 1000, 30000);

    std::string port = "/dev/ttyS6";
    Serial serial(port);
    // 打开串口
    if (!serial.open())
    {
        std::cerr << "Failed to open serial port: " << port << std::endl;
        return 1;
    }

    // 设置连接状态回调
    client.setConnectionCallback([&client](bool connected)
                                 {
        if (connected) {
            std::cout << "已连接到服务器" << std::endl;
        } else {
            std::cout << "与服务器的连接已断开，正在尝试重连..." << std::endl;
        } });

    // 连接到服务器
    if (!client.connect("8.152.4.127", 18843, client_id))
    {
        std::cerr << "首次连接服务器失败，将在后台尝试重连" << std::endl;
        // 继续执行，因为重连会在后台进行
    }

    // 设置消息回调
    client.setMessageCallback([&serial](const std::string &message)
                              {
        // 原有的消息处理代码...
        std::cout << "服务器消息: " << message << std::endl;
        
        // 简单解析JSON格式消息: {"controller_id":"1","lock_numble":"1"}
        std::string controller_id_key = "\"controller_id\":\"";
        std::string lock_numble_key = "\"lock_numble\":\"";
        
        size_t controller_pos = message.find(controller_id_key);
        size_t lock_pos = message.find(lock_numble_key);
        
        if (controller_pos != std::string::npos && lock_pos != std::string::npos) {
            // 解析逻辑...
            controller_pos += controller_id_key.length();
            lock_pos += lock_numble_key.length();
            
            size_t controller_end = message.find("\"", controller_pos);
            size_t lock_end = message.find("\"", lock_pos);
            
            if (controller_end != std::string::npos && lock_end != std::string::npos) {
                std::string controller_id_str = message.substr(controller_pos, controller_end - controller_pos);
                std::string lock_id_str = message.substr(lock_pos, lock_end - lock_pos);
                
                try {
                    int controller_id = std::stoi(controller_id_str);
                    int lock_id = std::stoi(lock_id_str);
                    
                    std::cout << "开锁请求: 控制板 " << controller_id << ", 锁号 " << lock_id << std::endl;
                    
                    // 调用开锁函数
                    bool result = serial.openLock(controller_id, lock_id);
                    if (result) {
                        std::cout << "成功打开锁 " << lock_id << " 在控制板 " << controller_id << std::endl;
                    } else {
                        std::cerr << "打开锁 " << lock_id << " 在控制板 " << controller_id << " 失败" << std::endl;
                    }
                } catch (const std::exception &e) {
                    std::cerr << "转换ID为整数时出错: " << e.what() << std::endl;
                }
            }
        } else {
            std::cerr << "无效的消息格式" << std::endl;
        } });

    std::cout << "客户端已启动，ID: " << client_id << std::endl;

    // 主线程处理用户输入并发送消息
    std::string input;
    while (true)
    {
        std::cout << "输入消息 (输入'exit'退出, 'status'查看连接状态): ";
        std::getline(std::cin, input);

        if (input == "exit")
            break;

        if (input == "status")
        {
            std::cout << "连接状态: " << (client.isConnected() ? "已连接" : "未连接") << std::endl;
            continue;
        }

        if (!client.sendMessage(input))
        {
            std::cerr << "消息发送失败" << std::endl;
        }
    }

    client.close();
    return 0;
}